from django.contrib import admin
from goods.models import GoodsType,IndexGoodsBanner,IndexPromotionBanner,IndexTypeGoodsBanner,GoodsSKU,Goods
from django.core.cache import cache
# Register your models here.

class Common_save_change(admin.ModelAdmin):
    def save_model(self,request,obj,form,change):
        # 保存时继续父类里面的嗯增删改查工作
        super().save_model(request,obj,form,change)
        # 附加生成静态页面的命令
        from celery_tasks.tasks import get_static_index
        get_static_index.delay()

        # 清楚缓存数据
        cache.delete('index_data')

    def delete_model(self,request,obj):
        #  保存时继续父类里面的嗯增删改查工作
        super().delete_model(request,obj)
        # 附加生成静态页面的命令
        from celery_tasks.tasks import get_static_index
        get_static_index.delay()

        # 清楚缓存数据
        cache.delete('index_data')

# 因为每一个模型管理类都需要上面的操作 所以给每一个继承
class GoodsTypeAdmin(Common_save_change):
    pass

class indexGoodsBannerAdmin(Common_save_change):
    pass

class IndexPromotionBannerAdmin(Common_save_change):
    pass

class IndexTypeGoodsBannerAdmin(Common_save_change):
    pass

class GoodsSKUAdmin(Common_save_change):
    pass

class GoodsAdmin(Common_save_change):
    pass

admin.site.register(GoodsType,GoodsTypeAdmin)
admin.site.register(IndexGoodsBanner,indexGoodsBannerAdmin)
admin.site.register(IndexPromotionBanner,IndexPromotionBannerAdmin)
admin.site.register(IndexTypeGoodsBanner,IndexTypeGoodsBannerAdmin)
admin.site.register(GoodsSKU,GoodsSKUAdmin)
admin.site.register(Goods,GoodsAdmin)